package com.yxh.www.author.service.impl;
import java.time.LocalDateTime;
import	java.util.ArrayList;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yxh.www.author.domain.SmUserObjectRelation;
import com.yxh.www.author.mapper.SmUserObjectRelationMapper;
import com.yxh.www.author.service.SmUserObjectRelationService;
import com.yxh.www.common.result.Result;
import com.yxh.www.common.result.ResultBuilder;
import com.yxh.www.common.result.ResultEnum;
import com.yxh.www.mysql.base.BaseServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统用户对象关系表 服务实现类
 * </p>
 *
 * @author Yangxiaohui
 * @since 2020-05-09
 */
@Service
public class SmUserObjectRelationServiceImpl extends BaseServiceImpl<SmUserObjectRelationMapper, SmUserObjectRelation> implements SmUserObjectRelationService {

    /**
     * 新增系统用户对象关系
     *
     * @param userId     用户ID    必填
     * @param objectIds  对象ID    必填
     * @param objectType 对象类型（0：角色；1：组织） 默认值0
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> addSmUserObjectRelation(String userId, String[] objectIds, String objectType) {
        if (objectIds.length== 0){
            return ResultBuilder.error(ResultEnum.USER_HANDEL_ERROR,"非法绑定对象ID");
        }
        // 遍历初始化插入实体列表
        List<SmUserObjectRelation> smUserObjectRelationList= new ArrayList<> ();
        // 获取当前时间
        LocalDateTime nowTime = LocalDateTime.now();
        for (String objectId:objectIds){
            smUserObjectRelationList.add(new SmUserObjectRelation()
                    .setUserId(userId)
                    .setObjectId(objectId)
                    .setObjectType(objectType)
                    .setCreateTime(nowTime)
            );
        }
        // 持久化数据
        baseMapper.insertList(smUserObjectRelationList);
        return ResultBuilder.success();
    }

    /**
     * 根据用户对象关系ID删除关系
     *
     * @param ids 关系ID集合
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmUserObjectRelation(String[] ids) {
        baseMapper.delete(new QueryWrapper<SmUserObjectRelation>().in("id",ids));
        return ResultBuilder.success();
    }

    /**
     * 根据用户ID删除 用户对象关系
     *
     * @param userId 用户ID    必填
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmUserObjectRelationByUserId(String userId) {
        baseMapper.delete(new QueryWrapper<SmUserObjectRelation>().eq("user_id",userId));
        return ResultBuilder.success();
    }

    /**
     * 根据对象ID删除 用户对象关系
     *
     * @param objectId 对象ID  必填
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmUserObjectRelationByObjectId(String objectId) {
        baseMapper.delete(new QueryWrapper<SmUserObjectRelation>().eq("object_id",objectId));
        return ResultBuilder.success();
    }

    /**
     * 根据用户ID和 对象ID 解除用户对象关系
     *
     * @param userId   用户ID    必填
     * @param objectId 对象ID    必填
     * @return 统一响应 {@link Result}
     */
    @Override
    public Result<Object> removeSmUserObjectRelationByUserIdAndObjectId(String userId, String objectId) {
        baseMapper.delete(new QueryWrapper<SmUserObjectRelation>()
                .eq("user_id",userId)
                .eq("object_id",objectId)
        );
        return ResultBuilder.success();
    }
}
